﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;


namespace Demos.TravelingSalesPerson
{
    /// <summary>
    /// Interaction logic for ArbolUserControl.xaml
    /// </summary>
    public partial class ArbolUserControl : UserControl
    {

        public ObservableCollection<ElementoVisual> ContenidoVisual
        {
            get { return (ObservableCollection<ElementoVisual>)GetValue(ContenidoVisualProperty); }
            set { SetValue(ContenidoVisualProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ContenidoVisual.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContenidoVisualProperty =
            DependencyProperty.Register("ContenidoVisual", typeof(ObservableCollection<ElementoVisual>), typeof(ArbolUserControl), new UIPropertyMetadata(null));


        public ArbolUserControl()
        {
            InitializeComponent();
        }

        private void arbolCvMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if ((DataContext as ViewModel).AñadirVerticeCommand.CanExecute(this))
            {
                (DataContext as ViewModel).X = e.GetPosition(control).X;
                (DataContext as ViewModel).Y = e.GetPosition(control).Y;
                (DataContext as ViewModel).AñadirVerticeCommand.Execute(this);
            }
            else
            {
                if ((DataContext as ViewModel).BorrarSeleccionCommand.CanExecute(this))
                {
                    (DataContext as ViewModel).BorrarSeleccionCommand.Execute(this);
                }
            }
        }

        private void VerticeMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var v = ((sender as Grid).Children[0] as Label).Content;
            (DataContext as ViewModel).VerticeSeleccionado = v as string;
            if ((DataContext as ViewModel).SeleccionarVerticeCommand.CanExecute(this))
            {
                (DataContext as ViewModel).SeleccionarVerticeCommand.Execute(this);
            }
            e.Handled = true;
        }

        private void AristaMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var a = ((sender as Grid).Children[0] as Label).Content;
            (DataContext as ViewModel).AristaSeleccionado = a as string;
            if ((DataContext as ViewModel).SeleccionarAristaCommand.CanExecute(this))
            {
                (DataContext as ViewModel).SeleccionarAristaCommand.Execute(this);
            }
            e.Handled = true;
        }

        private void EtiquetaMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var modificador = new EditarElemento(sender);
            modificador.ElementoModificado += (DataContext as ViewModel).ElementoModificado;
            modificador.Show();
            e.Handled = true;
        }

        private void controlSizeChanged(object sender, SizeChangedEventArgs e)
        {
            (DataContext as ViewModel).MaxX = control.ActualWidth;
            (DataContext as ViewModel).MaxY = control.ActualHeight;
        }
    }
}
